package net.solvetheriddle.wwclient.activity;

import net.solvetheriddle.wwclient.R;
import net.solvetheriddle.wwclient.WWLogic;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TabHost;

/**
 * @author anoniim
 * 
 */
public class MainActivity extends TabActivity {

    public static final int TAB_PROJECTLIST = 0;
    public static final int TAB_WORK_OVERVIEW = 1;
    public static final int TAB_STATISTICS = 2;
    
    public static final boolean offlineMode = false;
    
    // UI widgets
    private ImageView imageLabel;
    private TabHost tabHost;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      try {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // insertNewWorkManully();

        setupViews();
        setupTabs();
    } catch (Exception e) {
      // Log exception with the WWCLIENT tag
      Log.e("WWCLIENT", "! ERROR IN CODE: " + e.toString());
      e.printStackTrace();
    }
  }

  /**
   * Get reference to UI widgets
   */
  private void setupViews() {
    imageLabel = (ImageView) findViewById(R.id.animatedLabel);
    tabHost = getTabHost();
  }

  /**
   * set up the tabs
   */
  public void setupTabs() {
    TabHost.TabSpec spec;
    Intent intent;

    // first tab - List of projects
    intent = new Intent().setClass(this, ProjectListActivity.class);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
      intent.putExtras(extras);
    }

    // TODO: Tahat z resources
    // String projects = Resources.getSystem().getString(R.string.tab_projects);
    String projects = "projekty"; // getString(R.string.tab_projects);
    String workOverview = "prace"; // this.getString(R.string.tab_work_overview);
    String statistics = "statistiky"; // getString(R.string.tab_statistics);

    spec = tabHost.newTabSpec("projectTab").setIndicator(projects).setContent(intent);
    tabHost.addTab(spec);

    // second tab - Todays work overview
    intent = new Intent().setClass(this, WorkOverviewActivity.class);
    spec = tabHost.newTabSpec("workTableTab").setIndicator(workOverview).setContent(intent);
    tabHost.addTab(spec);

    // third tab - Statistics
    intent = new Intent().setClass(this, StatisticsGraphActivity.class);
    spec = tabHost.newTabSpec("statisticsTab").setIndicator(statistics).setContent(intent);
    tabHost.addTab(spec);
  }

  /**
   * not working anymore TODO need fix
   */
  @Deprecated
  private void insertNewWorkManully() {
    // try {
    // WWLogic.sendAllFinishedWorkToServer([project id], [description], [begin
    // time], [end time];
    // } catch (IOException e) {
    // e.printStackTrace();
    // } catch (ParsingException e) {
    // e.printStackTrace();
    // }
  }

  /**
   * Switches the tabs to the screen of given tabIndex. Use one of:
   * <ul>
   * <li>TAB_PROJECTLIST - Tab containing the list of projects</li>
   * <li>TAB_WORK_OVERVIEW - Tab containing todays work overview</li>
   * <li>TAB_STATISTICS - Tab containing various statistics</li>
   * </ul>
   * 
   * @param tabIndex index of the screen you want to switch to
   */
  public void switchTab(int tabIndex) {
    tabHost.setCurrentTab(tabIndex);
  }

  /**
   * Switches between red and green "eye" icon according to the working state
   */
  public void triggerWorkingLabel() {
    if (WWLogic.workInProgress()) {
      imageLabel.setImageResource(R.drawable.eye_green);
    } else {
      imageLabel.setImageResource(R.drawable.eye_red);
    }
  }
}
